define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/withdraw/index' + location.search,
                    // add_url: 'user/withdraw/add',
                    // edit_url: 'user/withdraw/edit',
                    // del_url: 'user/withdraw/del',
                    // multi_url: 'user/withdraw/multi',
                    import_url: 'user/withdraw/import',
                    table: 'withdraw',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);

                $("input[name='user_id']", form)
                    .addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "id")
                    .data("field", "nickname")
                    .data("orderBy", "id desc");

                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showToggle: false,
                commonSearch: true,
                showExport: true,
                searchFormVisible: true,
                search:false,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id'), visible:false, operate: 'LIKE %...%'},
                        {field: 'user.nickname', title: __('User_id'), operate: false},
                        {field: 'account', title: __('Account'), operate: 'LIKE'},
                        {field: 'bank', title: __('Bank'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'service_charge', title: __('Service_charge'), operate:'BETWEEN'},
                        {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                        {field: 'memo', title: __('Memo'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'reason', title: __('Reason'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons:[
                                {
                                    name: 'detail',
                                    text: '详情',
                                    title: '详情',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-sticky-note-o',
                                    url: function (res) {
                                        return 'user/withdraw/detail/ids/' + res.id;
                                    },
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        return true;
                                    }
                                },{
                                    name: 'agree',
                                    text: '同意',
                                    title: '同意',
                                    classname: 'btn btn-xs btn-success btn-view btn-ajax',
                                    icon: 'fa fa-check',
                                    url: function (res) {
                                        return 'user/withdraw/agree/ids/' + res.id;
                                    },
                                    visible: function (row) {
                                        if(row.status === 0){
                                            return true;
                                        }
                                    },
                                    refresh:true
                                },{
                                    name: 'reject',
                                    text: '驳回',
                                    title: '驳回',
                                    classname: 'btn btn-xs btn-danger btn-view btn-dialog',
                                    icon: 'fa fa-close',
                                    url: function (res) {
                                        return 'user/withdraw/reject/ids/' + res.id;
                                    },
                                    visible: function (row) {
                                        if(row.status === 0){
                                            return true;
                                        }
                                    },
                                    refresh:true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        },
        agree: function () {
            Controller.api.bindevent();
        },
        reject: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});